package com.xq.pethome.system.dept.controller;

import com.xq.pethome.system.dept.service.IDepartmentService;
import com.xq.pethome.system.dept.domain.Department;
import com.xq.pethome.system.dept.query.DepartmentQuery;
import com.xq.pethome.utils.AjaxResult;
import com.xq.pethome.utils.BusinessException;
import com.xq.pethome.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value  = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    public IDepartmentService departmentService;


    /**
     * 保存和修改公用的
     * @param department  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value  = "保存【有Id的为修改】" )
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if( department.getId()!=null)
                departmentService.update(department);
            else
                departmentService.add(department);
            return AjaxResult.success().setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value  = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success().setMsg("保存成功");
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {
            return AjaxResult.error().setMsg("网络异常，请联系管理员");
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value  = "通过ID查询" )
    public Department get(@PathVariable("id")Long id)
    {
        return departmentService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @ApiOperation(value  = "查看所有的员工信息" )
    public List<Department> list(){

        return departmentService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @ApiOperation(value  = "分页查询" )
    public AjaxResult json(@RequestBody DepartmentQuery query)
    {
        try {
            List<Department> departments = departmentService.findQuery(query);
            Long count = departmentService.findCount(query);
            return AjaxResult.success().setData(departments).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树" )
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }
    @PatchMapping("/del")
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchdel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("批量删除失败");
    }
    /**
     * excel导入
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file){
        List<Department> department = ExcelUtil.importExcel(file, 1, 1, Department.class);
        department.forEach(System.out::println);
    }
    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Department> depts = departmentService.findAll();
        ExcelUtil.exportExcel(depts,"员工列表","所有员工",Department.class,"数据导出.xls", response);
    }
}
